<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/3.3.7/css/bootstrap.min.css">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>


    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
    <style>

    </style>
</head>
<body>


<div id="vuebox">
    <div class="container">
        <div class="row" style="margin-top: 50px;">
            <div class="col-md-6">
                <form class="form-inline" role="form" style="padding-left: 250px;">
                    <div class="form-group" style="padding-left: 20px;">
                        <label>查找联系人:</label>
                        <input type="text" name="cname" v-model="cname">
                    </div>
                    <div class="form-group" style="padding-left: 20px;">
                        <button type="button" class="btn btn-primary" @click="selectContact(1)">查询</button>
                    </div>

                </form>
            </div>
            <div class="col-md-4 text-right">
                <a @click.stop="showModal()" class="btn btn-primary">添加联系人</a>
            </div>
        </div>

        <br/>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>序号</th>
                <th>姓名</th>
                <th>手机</th>
                <th>qq</th>
                <th>email</th>
                <th>地址</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="item in contactList.data.list">
                <td>{{item.id}}</td>
                <td><a @click="to_update_entry(item.id)">{{item.cname}}</a></td>
                <td>{{item.mobile}}</td>
                <td>{{item.qq}}</td>
                <td>{{item.email}}</td>
                <td>{{item.address}}</td>
            </tr>
            </tbody>
        </table>
        <br/>


        <div class="text-center">
            <ul class="pagination">
                <li v-show="contactList.data.hasPreviousPage">
                    <a href="#" @click.stop="selectContact(contactList.data.perPage)">&laquo;</a>
                </li>
                <li :class="{active:contactList.data.pageNum==i}" v-for="i of contactList.data.navigatepageNums">
                    <a href="#" @click.stop="selectContact(i)">{{i}}</a>
                </li>
                <li v-show="contactList.data.hasNextPage">
                    <a href="#" @click.stop="selectContact(contactList.data.nextPage)">&raquo;</a>
                </li>
            </ul>
        </div>
    </div>






<!--    添加模态框-->
    <!--模态框-->
    <div class="modal" id="mymodal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" type="button" @click="hideModal()">&times;</button>
                <div class="modal-title">
                    添加联系人
                </div>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">

                    <input type="text" v-model="new_contact.id" id=contact_id>

                    <div class="form-group">
                        <label for="cname" class="col-sm-2 control-label">姓名</label>
                        <div class="col-sm-10">
                            <input v-model="new_contact.cname" id="cname" type="text" class="form-control" placeholder="请输入图书名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="mobile" class="col-sm-2 control-label">手机</label>
                        <div class="col-sm-10">
                            <input v-model="new_contact.mobile" id="mobile" type="text" class="form-control" placeholder="请输入图书名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="qq" class="col-sm-2 control-label">qq</label>
                        <div class="col-sm-10">
                            <input v-model="new_contact.qq" id="qq" type="text" class="form-control" placeholder="请输入图书摘要"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-sm-2 control-label">email</label>
                        <div class="col-sm-10">
                            <input v-model="new_contact.email" id="email" type="text" class="form-control" placeholder="请输入上传人">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="address" class="col-sm-2 control-label">地址</label>
                        <div class="col-sm-10">
                            <input v-model="new_contact.address" id="address" type="text" class="form-control" placeholder="请输入上传时间">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" @click="hideModal()">关闭</button>
                <button class="btn btn-primary" @click="saveEntry()">保存</button>
            </div>
        </div>
    </div>
</div>


</div>

</body>
</html>
<script>
    var vm = new Vue({
        el: "#vuebox",
        data: {
            contactList:{},
            cname:"",
            delResult:{},
            new_contact:{id:"",cname:"",mobile:"",qq:"",email:"",address:""},
        },
        methods: {
            selectContact: function (pageNum) {
                axios.get("/pageInfo", {params: {cname: this.cname, pageNum: pageNum}}).then(res => {
                    this.contactList = res.data;
                    this.pageNum=pageNum;
                });
            },

            showModal:function () {
                $("#mymodal").modal("show");
            },
            hideModal:function () {
                $("#mymodal").modal("hide");
                //清除模态框内容
                this.clearForm();
            },
            clearForm:function () {
                $("#contact_id").val("");
                $("#cname").val("");
                $("#mobile").val("");
                $("#qq").val("");
                $("#email").val("");
                $("#address").val("");
            },
            saveEntry:function () {
            axios.post("/add",this.new_contact).then(res=>{
                this.delResult=res.data;
                if(this.delResult.result="SUCCESS"){
                    //增加成功默认到第一页
                    this.selectContact(this.pageNum);
                    //增加成功关闭模态框
                    this.hideModal();
                }else{
                    alert(this.delResult.message);
                }
            })
            },
            to_update_entry:function (id) {
                axios.get("/selectById",{params:{id:id}}).then(res=>{
                    this.new_contact=res.data.data;
                    this.showModal();
                })
            }
        },
        mounted() {
            this.selectContact(1);
        }
    })
</script>